package encaps.utils.rendering;

import encaps.resources.Response;
import static encaps.utils.html.HTML.*;
import encaps.utils.html.HTMLElement;
import encaps.utils.html.HTMLElementSequence;
import org.jdom.Element;

public class AbstractSelectFactory implements FieldFactory {

    @Override
    public HTMLElement renderField(Element e, boolean edit) {
        HTMLElement select = new HTMLElement(SELECT);
        String label = e.getChildTextTrim(Response.LABEL);
        String key = e.getChildTextTrim(Response.KEY);
        String value = e.getChildTextTrim(Response.VALUE);
        select.addAttribute(ID, label);
        HTMLElementSequence result = new HTMLElementSequence();
        if (key != null && value != null) {
            String[] sendvalues = key.split(";");
            String[] contents = value.split(";");
            HTMLElement option;
            for (int i = 0; i < sendvalues.length; i++) {
                option = new HTMLElement(OPTION);
                option.addAttribute(VALUE, sendvalues[i]);
                option.addContent(contents[i]);
                result.addContent(option);
            }
        }
        select.addContent(result);
        return select;
    }
}
